package com.zzuli.yxypre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzuli.yxypre.comment.R;
import com.zzuli.yxypre.entity.*;
import com.zzuli.yxypre.mapper.LoveMapper;
import com.zzuli.yxypre.mapper.SingerMapper;
import com.zzuli.yxypre.mapper.SongMapper;
import com.zzuli.yxypre.mapper.UserMapper;
import com.zzuli.yxypre.service.SingerService;
import com.zzuli.yxypre.service.UserService;
import com.zzuli.yxypre.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @auther pony
 * @create 2023-05-06 16:19
 */
@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {

    @Autowired
    private SingerMapper singerMapper;

    @Autowired
    private SongMapper songMapper;

    @Autowired
    private LoveMapper loveMapper;

    /**
     * 查询歌手分页信息
     * @param nav all:全部 A-Z:歌手首字母 #:其他
     * @param pageCurrent 当前页
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public R page(String nav, Integer pageCurrent, Integer pageSize) {
        Page<Singer> pageInfo = new Page<>(pageCurrent, pageSize);
        LambdaQueryWrapper<Singer> qw = new LambdaQueryWrapper<>();
        if ("all".equals(nav)){
            qw.eq(Singer::getSingerStatus, 0);
        }
        // 正则匹配
        String regStr = "[A-Z]";
        // 先创建一个Pattern对象，模式对象，可以理解成就是一个正则表达式对象
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(nav);
        if(matcher.find()){
            qw.eq(Singer::getSingerStatus, 0);
            qw.eq(Singer::getSingerPyname, nav);
        }else if(!"all".equals(nav)) {
            qw.eq(Singer::getSingerStatus, 0);
            qw.notIn(Singer::getSingerPyname, Arrays.asList('A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'));
        }
        Page<Singer> singerPage = singerMapper.selectPage(pageInfo, qw);
        return R.success().message("歌手分页信息查询成功！").data("singerPage", singerPage);
    }

    /**
     * 根据 歌手 id 获取歌手信息
     * @param id
     * @return
     */
    @Override
    public R getOneById(Integer id, HttpServletRequest request) {
        // 获取登录用户id
        Integer userId = Integer.valueOf(JwtUtils.getUserIdByJwtToken(request));
        
        LambdaQueryWrapper<Singer> qw = new LambdaQueryWrapper<>();
        qw.eq(Singer::getSingerId, id);
        Singer singer = singerMapper.selectOne(qw);
        // 封装歌手的所有歌曲
        List<Song> songList = singerMapper.selectSongBySingerId(id);
        // 判断歌曲是否被当前登录用户所喜欢
        for (Song song : songList) {
            LambdaQueryWrapper<Love> loveQw = new LambdaQueryWrapper<>();
            // 歌曲 type 是 1
            loveQw.eq(Love::getType, 1);
            loveQw.eq(Love::getUserId, userId);
            loveQw.eq(Love::getRelevance, song.getSongId());
            Love love = loveMapper.selectOne(loveQw);
            if (!Objects.isNull(love)){
                song.setIsLove(1);
            }else {
                song.setIsLove(0);
            }
        }
        singer.setSongList(songList);
        // 封装歌手的歌曲的专辑信息
        for (Song song : songList) {
            List<Album> albumList = songMapper.selectAlbumBySongId(song.getSongId());
            song.setAlbum(albumList);
        }
        // 封装歌手的歌曲的歌手信息
        for (Song song : songList) {
            List<Singer> singerList = songMapper.selectSingerBySongId(song.getSongId());
            song.setSinger(singerList);
        }

        // 封装歌手的专辑信息
        List<Album> albumList = singerMapper.selectAlbumBySingerId(id);
        singer.setAlbumList(albumList);

        return R.success().message("单个歌手信息").data("singerInfo", singer);
    }
}
